from django.db import models


# Create your models here.


class It(models.Model):
    """
    接口项目表
    """
    COLOR_CHOICES = (
        (1, "#56b8eb"),
        (2, "#f28033"),
        (3, "#ebc656"),
        (4, "#a2d148"),
        (5, "#20BFA4"),
        (6, "#7461c2"),
        (7, "#20bfa3"),
    )

    it_name = models.CharField(max_length=32, default="", verbose_name="项目名称")
    it_desc = models.TextField(max_length=255, default="", verbose_name="项目描述")
    it_url = models.CharField(max_length=255, default="", verbose_name="测试URL")
    color = models.SmallIntegerField(verbose_name="颜色", choices=COLOR_CHOICES, default=1)
    it_start_time = models.DateField(verbose_name="开始时间")
    it_end_time = models.DateField(verbose_name="结束时间")

    def __str__(self):
        return self.it_name

    def coverage_rate(self):
        """
        覆盖率
        :return:
        """
        if self.api_set.count() and self.api_set.filter(api_pass_status=1).count():
            result = self.api_set.filter(api_pass_status=1).count() / self.api_set.count() * 100
            return "%.2f%%" % result
        else:
            return "0"

    @classmethod
    def get_all(cls):
        return cls.objects.all().count()


class Api(models.Model):
    """
    接口用例表
    """
    api_sub_it = models.ForeignKey(to="It", verbose_name="所属接口项目", on_delete=models.CASCADE)
    api_name = models.CharField(max_length=32, default="", verbose_name="用例名称")
    api_title = models.CharField(max_length=255, default="", verbose_name="用例标题")
    api_desc = models.CharField(max_length=255, null=True, blank=True, default="", verbose_name="用例描述")
    api_url = models.CharField(max_length=255, default="", null=True, blank=True, verbose_name="接口URL")
    REQUEST_TYPE = (
        (1, "GET"),
        (2, "POST")
    )
    api_method = models.SmallIntegerField(choices=REQUEST_TYPE, default=1, verbose_name="请求类型")
    api_params = models.CharField(max_length=255, null=True, blank=True, default={}, verbose_name="请求参数params")
    api_data = models.CharField(max_length=255, null=True, blank=True, default={}, verbose_name="请求data")
    api_json = models.CharField(max_length=255, null=True, blank=True, default={}, verbose_name="请求json")
    api_cookies = models.CharField(max_length=255, null=True, blank=True, default="", verbose_name="请求cookies",
                                   help_text="处理请求依赖cookies")
    api_headers = models.CharField(max_length=255, null=True, blank=True, default={}, verbose_name="请求头",
                                   help_text="处理依赖请求")
    api_expect = models.CharField(max_length=4196, null=True, blank=True, default={}, verbose_name="预期结果")
    api_temporary_request_headers = models.CharField(max_length=255, null=True, blank=True, verbose_name="获取请求头")
    api_temporary_request_data = models.CharField(max_length=255, null=True, blank=True, verbose_name="获取请求参数data")
    api_temporary_request_json = models.CharField(max_length=255, null=True, blank=True, verbose_name="获取请求的json")
    api_temporary_request_params = models.CharField(max_length=255, null=True, blank=True, verbose_name="获取请求的params")
    api_temporary_response_cookies = models.CharField(max_length=255, null=True, blank=True, verbose_name="响应cookies")
    api_temporary_response_headers = models.CharField(max_length=500, null=True, blank=True, verbose_name="响应headers")
    api_temporary_response_json = models.CharField(max_length=255, null=True, blank=True, verbose_name="响应json")
    api_report = models.TextField(verbose_name="报告", default="")
    api_run_time = models.DateTimeField(null=True, verbose_name="执行时间")
    ascending_order = models.IntegerField(null=True, blank=True, verbose_name="排序", default=0)
    API_PASS_STATUS_CHOICES = (
        (0, "未通过"),
        (1, "已通过")
    )

    api_pass_status = models.SmallIntegerField(choices=API_PASS_STATUS_CHOICES, verbose_name="执行是否通过", default=0)
    API_RUN_STATUS_CHOICES = (
        (0, "未执行"),
        (1, "已执行"),
    )
    api_run_status = models.SmallIntegerField(choices=API_RUN_STATUS_CHOICES, verbose_name="是否执行", default=0)

    def __str__(self):
        return self.api_name


class Logs(models.Model):
    """
    用例执行记录
    1、所属项目
    2、执行时间
    3、执行的测试报告
    4、通过多少、失败多少、共执行了多少用例、通过率是多少
    """
    log_report = models.TextField(verbose_name="报告", default="")
    log_sub_it = models.ForeignKey(to="It", verbose_name="所属接口的项目", on_delete=models.CASCADE)
    log_run_time = models.DateTimeField(null=True, verbose_name="log日志产生时间", auto_now_add=True)  # 自动添加当前时间
    log_pass_count = models.IntegerField(verbose_name="通过数量")
    log_failed_count = models.IntegerField(verbose_name="失败数量")
    log_errors_count = models.IntegerField(verbose_name="报错数量")
    log_run_count = models.IntegerField(verbose_name="执行用例总数量")

    def pass_rate(self):
        """通过率"""
        if self.log_run_count:
            result = self.log_pass_count / self.log_run_count * 100
            if result:
                return "%.f%%" % (result)
            else:
                return 0
        else:
            return 0

    class Meta:
        ordering = ["-log_run_time"]


class Wiki(models.Model):
    title = models.CharField(verbose_name="标题", max_length=32)
    project = models.ForeignKey(verbose_name="项目", to="It", on_delete=models.CASCADE)
    content = models.TextField(verbose_name="内容")
    depth = models.IntegerField(verbose_name="深度", default=1)

    def __str__(self):
        return self.title


class FileRepository(models.Model):
    """
    文件库
    """
    project = models.ForeignKey(to="It", on_delete=models.CASCADE)
    file_type_choices = (
        (1, "文件"),
        (2, "文件夹")
    )
    file_type = models.SmallIntegerField(verbose_name="类型", choices=file_type_choices)
    name = models.CharField(verbose_name="文件夹名称", max_length=32, help_text="文件/文件名")
    file_rename = models.CharField(verbose_name="别名", max_length=255)
    file_size = models.BigIntegerField(verbose_name="文件大小", null=True, blank=True, help_text="字节")
    file_path = models.CharField(verbose_name="文件路径", max_length=255, null=True, blank=True)
    parent = models.ForeignKey(verbose_name="父级目录", to="self", related_name="child", null=True, blank=True,
                               on_delete=models.CASCADE)
    update_datetime = models.DateTimeField(verbose_name="更新时间", auto_now=True)


class Domain(models.Model):
    """
    域名管理
    """
    project = models.ForeignKey(to="It", on_delete=models.CASCADE)
    TESTING_ENVIRONMENT = (
        (1, "内测"),
        (2, "封测"),
        (3, "线上")
    )
    SITE_TYPE = (
        (1,"官网"),
        (2,"后台"),
        (3,"CRM官网"),
        (5,"其它")
    )
    url_name = models.CharField(verbose_name="url", max_length=255)
    context = models.SmallIntegerField(verbose_name="测试环境",choices=TESTING_ENVIRONMENT,default=1)
    site_type = models.SmallIntegerField(verbose_name="网站类型",choices=SITE_TYPE)
    comment = models.TextField(verbose_name="备注",blank=True,null=True)
    update_datetime = models.DateTimeField(verbose_name="更新时间", auto_now=True)


class UploadApk(models.Model):
    """
    apk管理
    """
    TESTING_ENVIRONMENT = (
        (1, "内测"),
        (2, "封测"),
        (3,"线上")
    )
    project = models.ForeignKey(to="It", on_delete=models.CASCADE)
    apk_file = models.CharField(verbose_name="apk文件名",max_length=255,blank=True,null=True)
    versions = models.CharField(verbose_name="版本",max_length=32,blank=True,null=True)
    context = models.SmallIntegerField(verbose_name="测试环境", choices=TESTING_ENVIRONMENT, default=1)
    file_path = models.CharField(verbose_name="文件路径", max_length=255, null=True, blank=True)
    comment = models.TextField(verbose_name="备注", default="",blank=True,null=True,max_length=255)
    update_datetime = models.DateTimeField(verbose_name="更新时间", auto_now=True)


